Consul Hystrix Dashboard


Hystrix Dashboard 即 “Hystrix 仪表盘”,也是除了容错之外还提供的实时监控功能,他会实时的累加记录所有关于 HystrixCommand 执行的信息,其中包含了每秒执行了多少请求,以及成功或失败等信息。

Hystrix Dashboard 是一款针对 Hystrix 进行实时监控的工具,可以实现出实时监控数据,并直观的显示 Hystrix Command 的请求响应时间、请求成功率等

因为 Hystrix Dashboard 仅仅实现了数据的监控,因此 我们需要在 Hystrix Fallback 的基础上进行扩展,其中启动类需要修改并添加依赖:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix-dashboard -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.example.demo;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

/**
* 启用服务发现以及 feign、Hystrix Dashboard 和服务熔断等注解支持,并配置 Servlet
*
* @author kunlun
* @date 2021/7/6
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard
@EnableCircuitBreaker
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

@Bean
public ServletRegistrationBean servletRegistrantionBean() {
HystrixMetricsStreamServlet hystrixMetricsStreamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(hystrixMetricsStreamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}

application.properties

1
2
3
4
5
6
7
spring.application.name=HystrixDashboard
server.port=9502
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.service-name=service-provider
feign.hystrix.enabled=true
hystrix.dashboard.proxy-stream-allow-list=* # 设置仪表板代理流允许列表为所有

当一切完成之后访问 http://localhost:9502/hystrix 并在输入框输入 http://localhost:9502/actuator/hystrix.stream 即可。

Hystrix Turbine 与 Hystrix Dashboard 的区别就是一个只能看到单个应用程序内的服务信息,而另一个则是多个实例集群的状态形式展现,因此 Hystrix Turbine 由此而生。

强烈建议使用 Eureka

本文使用《江雪分析公开知识存储库知识共享许可证》进行发布